package com.hk.service.impl;

import com.hk.commons.ConstantParm;
import com.hk.commons.HqlFilter;
import com.hk.commons.HttpUtils;
import com.hk.commons.StringHelper;
import com.hk.commons.enm.MsgType;
import com.hk.commons.entity.Parameter;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.PushMsg;
import com.hk.service.IPush;
import com.hk.service.order.IParameter;
import com.hk.service.sys.ISysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Service
public class PushImpl implements IPush {
    private Logger logger = LoggerFactory.getLogger(PushImpl.class);

    @Autowired
    private ISysUser userService;

    @Value("${push.url.licaishi}")
    private String url;

    @Value("${push.url.huacaitong}")
    private String customerUrl;

    @Value("${push.env}")
    private String env;

    @Value("${push.tag}")
    private String tag;


    @Autowired
    private IParameter parmService;

    // aliases,tags,registIds多个的话以逗号隔开。
    @Override
    public boolean jgPushMsg(String msg, String platForm,
                             String tags, String registIds, String phone, String menu, String msgTypeConstants) {
        logger.info("极光推送给理财师phone={},msg={},platForm={},tags={},registIds={}",
                phone, msg, platForm, tags, registIds);
        if(isDisablePush())
            return true;

        boolean pushResult = false;

        HqlFilter filter = new HqlFilter();
        filter.addEQFilter("name", phone);
        filter.addEQFilter("closeAccount",0);
        List<SysUser> users = userService.findByFilter(filter);

        if (users == null || users.size() <= 0)
              return false;

        SysUser user = users.get(0);
        Integer financialPhoneType = user.getMobileType();
        if (financialPhoneType == null)
            return false;

        String aliases = String.valueOf(user.getId());

        try {

            PushMsg pushMsg = new PushMsg();
            pushMsg.setEnv(env);
            pushMsg.setMenu(menu);
            pushMsg.setMsg(msg);

            MsgType msgType = MsgType.findByCode(msgTypeConstants);
            pushMsg.setTitle(msgType.getType());

            pushMsg.setAliases(aliases);
            pushMsg.setRegistIds(registIds);
            pushMsg.setMsgTypeConstants(msgTypeConstants);
            pushMsg.setChannel(2);

            pushMsg.setUuid(UUID.randomUUID().toString());
            if(StringHelper.isNotEmpty(tag)){
                pushMsg.setTags(tag);
            }

            pushResult = HttpUtils.pushMsgToUser(url, pushMsg);
        } catch (Exception e) {
            logger.error("极光推送失败", e);
        }

        logger.info("极光推送给理财师发送结果result={},msg={},phone={} ", pushResult, msg, phone);
        return pushResult;
    }


    @Override
    public boolean jgPushCustomerMsg(@RequestParam String msg, String title, String platForm, String tags, String registIds, String phone, String menu, String msgTypeConstants) {
        logger.info("极光推送给客户phone={},msg={},platForm={},tags={},registIds={}",
                phone, msg, platForm, tags, registIds);

        if(isDisablePush())
            return true;

        boolean pushResult = false;

        HqlFilter filter = new HqlFilter();
        filter.addEQFilter("name", phone);
        filter.addEQFilter("closeAccount",0);
        List<SysUser> users = userService.findByFilter(filter);

        if (users == null || users.size() <= 0)
            return false;

        SysUser user = users.get(0);

        String aliases = String.valueOf(user.getId());

        try {

            PushMsg pushMsg = new PushMsg();
            pushMsg.setEnv(env);
            pushMsg.setMenu(menu);
            pushMsg.setMsg(msg);
            pushMsg.setTitle(title);
            pushMsg.setAliases(aliases);
            pushMsg.setRegistIds(registIds);
            pushMsg.setMsgTypeConstants(msgTypeConstants);
            pushMsg.setChannel(1);

            pushMsg.setUuid(UUID.randomUUID().toString());
            if(StringHelper.isNotEmpty(tag)){
                pushMsg.setTags(tag);
            }

            pushResult = HttpUtils.pushMsgToUser(customerUrl, pushMsg);
        } catch (Exception e) {
            logger.error("极光推送失败", e);
        }

        logger.info("极光推送给客户发送结果result={},msg={},phone={} ", pushResult, msg, phone);
        return pushResult;
    }

    private boolean isDisablePush() {

        Parameter param = parmService.getByName(ConstantParm.DISABLE_PUSH_MSG);

        if (param == null || (!param.getValue01().equalsIgnoreCase("YES"))) {
            return false;
        }

        if(param.getValue01().equalsIgnoreCase("YES")){
            logger.info("后台参数: {}=YES, 禁用消息推送 !!~ ",ConstantParm.DISABLE_PUSH_MSG);
           return true;
        }

        return false;
    }
}
